<?php

namespace app\admin\controller\task;

use app\common\library\Auth;
use app\common\controller\Backend;
use think\Db;
use fast\Random;

/**
 * 订单管理
 *
 * @icon fa fa-user
 */
class Flower extends Backend
{
    /**
     * Order模型对象
     * @var \app\admin\model\shop\Order
     */
    protected $model = null;

    /**
     * 快速搜索时执行查找的字段
     */
    protected $searchFields = 'order_id';

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\shop\Order;
        $this->view->assign("orderStateList", $this->model->getOrderStateList());
        $this->view->assign("flagList", $this->model->getFlagList());
        $this->view->assign("roomList", $this->model->getRoomList());
        $this->view->assign("authorStatusList", $this->model->getAuthorStatusList());
    }


    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */


    /**
     * 查看
     */
    public function index()
    {
        //当前是否为关联查询
        $this->relationSearch = false;
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $_where["o.task_type"] = "3";
            if(session('admin')['athur_id']==1) {
                $_where['k.shop_admin_id'] = session('admin')['athor'];
            }else{
                $_where['k.shop_admin_id'] = session('admin')['id'];
            }
            $join[] = ["sd_shop_task k","k.task_id = o.task_id ","left"];//任务表
//            $join[] = ["sd_admin n","n.id = o.shop_admin_id","left"];//管理员表
//            $join[] = ["sd_user r","r.uid = o.uid","left"];//用户表
//            $join[] = ["sd_admin a","a.id = o.author","left"];//管理员
            $field = "o.*,order_id as id";//订单字段
            $field .= ",k.task_sno,k.platform_type";//任务
//            $field .= ",r.username as rusername";//用户表
//            $field .= ",n.username as nusername";//管理员表
//            $field .= ",a.username as ausername";//管理员表
            $total = $this->model
                ->alias("o")
                ->join($join)
                ->where($where)
                ->where($_where)
                ->order($sort, $order)
                ->count();
            $list = $this->model
                ->alias("o")
                ->join($join)
                ->field($field)
                ->where($where)
                ->where($_where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();
            $list = collection($list)->toArray();
            foreach ($list as &$value){
//                $value["r.username"] = $value["rusername"];
//                $value["n.username"] = $value["nusername"];
//                $value["a.username"] = $value["ausername"];
                $value["o.status"] = $value["status"];
                $value["o.create_time"] = $value["create_time"];
                $value["o.shop_name"] = $value["shop_name"];
            }
            $result = array("total" => $total, "rows" => $list);
            return json($result);
        }
        return $this->view->fetch();
    }


    /**
     * 插旗,详情
     */
    public function edit($ids = NULL){
        $row = $this->model
            ->with(['task'=>function($q){
                $q->with('shop');
            },'orderImage'])
            ->field('*,order_id as id')
            ->where("order_id",$ids)
            ->find();

        //,'order_image'
//        if($row["status"]<=2){
//            $row["order_image"] = Db::name("order_image")->where("order_id",$ids)->where("status_type",1)->find();
//        }else {
//            $row["order_image"] = Db::name("order_image")->where("order_id",$ids)->where("status_type",2)->find();
//        }
        //halt($row->toArray());
//        if ($this->request->isPost())
//        {
//            $params = $this->request->post("row/a");
//
//            //halt($params);
//
//            if ($params) {
//                $shop_order_info = Db::name("shop_order")->where(["order_id"=>$params["order_id"]])->find();
//
//                $flag = $shop_order_info["flag"];
//
//                if($params["type"]==1){
//
//                    $this->model->save(["flag_remarks"=>$params["flag_remarks"],"flag"=>"2","flag_time"=>time(),"update_time"=>time()],["order_id"=>$params["order_id"]]);
//
//                    if($flag==1) {
//                        Db::name("shop_task")->where("task_id", $shop_order_info["task_id"])->setInc("flag_child_count", 1);
//                    }
//
//                }
//
//            }
//
//        }
        $this->view->assign("row", $row);
//        $this->view->assign("type",input("type"));
        return $this->view->fetch();
    }


}